package com.lanqiao.attendance.controller;

import com.lanqiao.attendance.pojo.Class;
import com.lanqiao.attendance.pojo.Student;
import com.lanqiao.attendance.service.ClassService;
import com.lanqiao.attendance.service.StudentService;
import com.lanqiao.attendance.vo.ClassPartVo;
import com.lanqiao.attendance.vo.Result;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("class")
public class ClassController {

    @Autowired
    ClassService classService;
    @Autowired
    StudentService studentService;

    /**
     * 插入班级信息
     * @param cls
     * @return
     */
    @PostMapping("insert")
    public Result insertClass(@RequestBody Class cls){
        System.out.println(cls);
        Class cls1 = classService.queryClassAllByName(cls.getName());
        if (cls1!=null){
            return Result.fail(-999,"插入失败,已存在该班级名,请更换信息重新插入");
        }
        int i = classService.insertClass(cls);
        cls1 = classService.queryClassAllByName(cls.getName());
        if (cls1==null){
            return Result.fail(-999,"插入失败");
        }
        return Result.success("插入成功",cls1);
    }


    /**
     * 管理员通过班级id删除班级
     * @param clazz
     * @return
     */
    @PostMapping("delete/ClassById")
    public Result deleteClassById(@RequestBody Class clazz){
        String className = classService.queryClassAllById(clazz.getId()).getName();
        System.out.println(className);
       studentService.rmFromClassByName(className);
        long id = clazz.getId();
        Class cls = classService.queryClassAllById(id);
        if (cls==null){
            return Result.fail(-999,"删除失败");
        }
        int i = classService.deleteClassById(id);
        return Result.success("删除成功",cls);
    }

    /**
     * 管理员根据班级id更新班级信息
     * @param cls
     * @return
     */
    @PostMapping("update/ClassAllById")
    public Result updateClassAllById(@RequestBody Class cls){

        Long id = cls.getId();
        String newName = cls.getName();
        Class aClass = classService.queryClassAllById(id);
        String oldName = aClass.getName();
        if(!oldName.equals(newName)){
            studentService.updateClassName(oldName,newName);
        }
        int i = classService.updateClassAllById(cls);
        System.out.println(cls);
       Class cls1 = classService.queryClassAllById(cls.getId());
       return Result.success("更新成功",cls);
    }


    /**
     * 管理员查看所有班级的所有信息
     * @return
     */
    @GetMapping("query/AllClassAll")
    public Result  queryAllClassAll(){
        return  Result.success("查询成功",classService.queryAllClassAll());
    }

    /**
     *  管理员根据id查询班级全部信息
     * @param id
     * @return
     */
    @GetMapping("query/ClassAllById")
    public Result queryClassAllById(long id){
        Class cls =classService.queryClassAllById(id);
        if(cls==null){
            return Result.fail(-999,"查询失败,无该班级");
        }
        return Result.success("查询成功",cls);
    }

    /**
     * 管理员根据名称查询该班级的全部信息
     * @param name
     * @return
     */
    @GetMapping("query/ClassAllByName")
    public  Result queryClassAllByName(String name){
        Class cls = classService.queryClassAllByName(name);
        if (cls==null){
            return Result.fail(-999,"没有查询到此班级");
        }
        return Result.success("查询成功",cls);
    }

    /**
     * 学生或教师查询全部班级部分信息
     * @return
     */
    @GetMapping("query/AllClassPart")
    public Result  queryClassAll(){
        List<ClassPartVo> classes = classService.queryClassAll();
        if (classes==null){
            return Result.fail(-999,"查询失败，当前无班级");
        }
        return Result.success("查询成功",classes);
    }
    /**
     * 学生或教师根据名称查询班级的部分信息
     * @param name
     * @return
     */
    @GetMapping("query/ClassPartByName")
    public Result queryClassPartByName(String name){
        ClassPartVo cls = classService.queryClassPartByName(name);
        if (cls==null){
            return Result.fail(-999,"无该班级信息");
        }
        return Result.success("查询成功",cls);
    }

    @GetMapping("query/classByTeaName")
    public Result queryByTeacherName(@RequestParam("name") String teaName) {
        List<Class> classList = classService.queryByTeacherName(teaName);
        if (classList.size() > 0) {
            return Result.success("查询["+teaName+"]教师所授班级成功",classList);
        } else {
            return Result.fail(-999,"没有该教师的相关班级信息");
        }
    }

}
